package com.sikadwafastdaisfondturbo.pretloan.utils.helper

import com.sikadwafastdaisfondturbo.pretloan.BuildConfig
import com.sikadwafastdaisfondturbo.pretloan.utils.config.Configs
import org.json.JSONObject

/**
 * ConfigsHelper
 * @Author wwdeng
 * @CreateTime: 2024/11/28 12:04
 * @Annotation：客服配置 自定义数据 帮助类
 */
object SikadwaKefuConfigHelper {
    //缓存弹出公告信息的次数，此值只会在：审核中、放款中、待还款改变
    var showNoticeNum = 0

    /**
     * 自定义数据来源
     *     1：客服配置
     *
     * "customData": {
     *         "workDate": "Monday to Sunday",
     *         "workTime": "9:30am to 19:30pm.",
     *         "workDate_sw": "Jumatatu hadi Jumapili",
     *         "workTime_sw": "9:30 asubuhi - 19:30 jioni",
     *         "guideType": "shoLoanAmount",//权限页 和 进件信息页 激励引导类型： showLoanAmount / showPassRate
     *         "ocrNoDocuments": "YES",//是否支持选择不传证件照
     *         "defaultLang": "en",//默认语言
     *         "noticeInfo": {
     *             "num": 3,
     *             "isShow": "YES",
     *             "noticeTitle": "Notice",
     *             "noticeText": "Please do not transfer repayments to any private account to avoid fraud and loss!"
     *         },
     *         "noticeInfo_sw": {
     *             "num": 3,
     *             "isShow": "YES",
     *             "noticeTitle": "Taarifa",
     *             "noticeText": "Tafadhali usihamishe malipo kwa akaunti yoyote ya kibinafsi ili kuepuka ulaghai na hasara!"
     *         },
     *         "release": {
     *             "isMustUpdate": false,
     *             "versionCode": "17",
     *             "versionName": "1.0.17",
     *             "versionRemark": "1.A brand new UI design.$2.Update more secure data processing.$3.Resolve known bugs, optimize code, and enhance user experience."
     *         },
     *         "release_sw": {
     *             "isMustUpdate": false,
     *             "versionCode": "9",
     *             "versionName": "9.0.0",
     *             "versionRemark": "1.Muundo mpya kabisa wa UI.$2.Sasisha uchakataji salama zaidi wa data.$3.Tatua hitilafu zinazojulikana, boresha misimbo, na uboreshe matumizi ya mtumiaji."
     *         }
     *     },
     */

    /**
     * 获取客服配置
     */
    fun takeSikadwaKefuConfigData(): JSONObject? {
        val dataString = Configs.getSPString(Configs.SPKEY.SIKADWA_COMMON)
        if (dataString.isEmpty()){
            return null
        }
        val data = JSONObject(dataString)
        if (data.has("customize")){
            return data.optJSONObject("customize")
        }
        return null
    }

    /**
     * 获取客服配置里自定义数据
     * customData
     */
    fun takeSikadwaKefuCustomData(): JSONObject?{
        val customize = takeSikadwaKefuConfigData()
        if (customize != null) {
            return customize.optJSONObject("customData")
        }
        return null
    }

    /**
     * 获取客服工作日期
     */
    fun takeSikadwaKefuWorkDate(): String{
        val customData = takeSikadwaKefuCustomData() ?: return ""

        var workDate = ""
        if (customData.has("workDate")){
            workDate = customData.optString("workDate")
        }
        //斯瓦希里语
        if (Configs.getSPString(Configs.SPKEY.SIKADWA_LANGUAGE_CHOOSE) == "sw"){
            if (customData.has("workDate_sw")){
                //默认为英语
                workDate = customData.optString("workDate_sw")
            }
        }
        return workDate
    }

    /**
     * 获取客服工作时间
     */
    fun takeSikadwaKefuWorkTime(): String{
        val customData = takeSikadwaKefuCustomData() ?: return ""

        var workTime = ""
        if (customData.has("workTime")){
            workTime = customData.optString("workTime")
        }
        //斯瓦希里语
        if (Configs.getSPString(Configs.SPKEY.SIKADWA_LANGUAGE_CHOOSE) == "sw"){
            if (customData.has("workTime_sw")){
                //默认为英语
                workTime = customData.optString("workTime_sw")
            }
        }
        return workTime
    }

    /**
     * 获取默认配置语言
     */
    fun takeSikadwaDefConfigLang(): String{
        val customData = takeSikadwaKefuCustomData() ?: return ""
        return customData.optString("defaultLang","")
    }

    /**
     * 获取是否支持不传证件照
     */
    fun takeSikadwaOcrNoDocuments(): Boolean{
        val customData = takeSikadwaKefuCustomData() ?: return false
        return customData.optString("ocrNoDocuments","") == "YES"
    }

    /**
     * 获取 权限页、进件信息页 引导类型
     * guideType：showLoanAmount / showPassRate
     */
    fun takeSikadwaGuideType(): String{
        //默认 showLoanAmount
        val customData = takeSikadwaKefuCustomData() ?: return "showLoanAmount"
        return customData.optString("guideType","showLoanAmount")
    }

    /**
     * 获取 审核中、放款中 显示的公告信息
     */
    fun takeSikadwaKefuNoticeInfo(): JSONObject?{
        val customData = takeSikadwaKefuCustomData() ?: return null

        var noticeData: JSONObject? = null
        if (customData.has("noticeInfo")){
            //默认为英语
            noticeData = customData.optJSONObject("noticeInfo")
        }
        //斯瓦希里语
        if (Configs.getSPString(Configs.SPKEY.SIKADWA_LANGUAGE_CHOOSE) == "sw"){
            if (customData.has("noticeInfo_sw")){
                //默认为英语
                noticeData = customData.optJSONObject("noticeInfo_sw")
            }
        }
        return noticeData
    }

    /**
     * 审核中、放款中、待还款是否显示公告信息
     */
    fun isSikadwaShowNotice(): Boolean{
        val noticeInfo = takeSikadwaKefuNoticeInfo() ?: return false
        if (noticeInfo.has("isShow")){
            return noticeInfo.optString("isShow","NO") == "YES"
        }
        return false
    }

    /**
     * 获取版本更新信息
     */
    fun takeSikadwaUpdateData(): JSONObject?{
        val customData = takeSikadwaKefuCustomData() ?: return null
        if (!customData.has("release")){
            return null
        }
        var releaseData = customData.optJSONObject("release")
        //斯瓦希里语
        if (Configs.getSPString(Configs.SPKEY.SIKADWA_LANGUAGE_CHOOSE) == "sw"){
            if (customData.has("release_sw")){
                releaseData = customData.optJSONObject("release_sw")
            }
        }
        return releaseData
    }

    /**
     * 获取是否有强制更新（启动页使用）
     *  "isMustUpdate":true,
     *  "versionCode":"1",
     *  "versionName":"1.0.0",
     *  "versionRemark":"1xxx$2xxx$3xxx"
     */
    fun isSikadwaShowUpdateBreak(): Boolean {
        val versionObj = takeSikadwaUpdateData() ?: return false
        val versionCode = versionObj.optInt("versionCode",1)
        val isMustUpdate = versionObj.optBoolean("isMustUpdate",false)
        return BuildConfig.VERSION_CODE < versionCode && isMustUpdate
    }

    /**
     * 获取是否有更新（首页使用）
     */
    fun isSikadwaShowUpdate(): Boolean{
        val versionObj = takeSikadwaUpdateData() ?: return false
        val versionCode = versionObj.optInt("versionCode",1)
        return BuildConfig.VERSION_CODE < versionCode
    }
}